import DB.DB;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.concurrent.ArrayBlockingQueue;

public class PageJFrame extends JFrame {
    private JLabel save,history,newWindow;
    private JTextField inputText,numberText,times,bigText,algorithmText;
    private JTextField name,no,algorithmName,brickNum,suspendNum,middle;
    private JLabel input,select,clear,animation,start;
    private JLabel message1,message2;
    private JLabel saveWindow,exit,saveButton;
    private MyTableModel model;
    private JTable selectTable,table;
    private boolean inputTextOK,numberTextOK,readyOK,chooseOk;
    private ArrayBlockingQueue <String>queue;
    private LinkedList <String>list;
    private PageJFrame(){
        super("页面置换算法");
        //窗口高度要比背景高30px
        this.setSize(1200,930);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        ImageIcon img = new ImageIcon("images/背景.png");//这是背景图片
        JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
        this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//背景标签添加
        imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());//设置背景标签的位置
        Container cp=this.getContentPane();
        cp.setLayout(null);
        ((JPanel)cp).setOpaque(false); //注意这里，将内容面板设为透明

        inputTextOK = false;
        numberTextOK = false;
        readyOK = false;
        chooseOk = false;

        /*
        * 弹出窗口
        * */
        name = new JTextField();
        name.setBounds(539,377,200,40);
        beautifyTextField(name);
        name.setVisible(false);
        cp.add(name);

        no = new JTextField();
        no.setBounds(540,428,200,40);
        beautifyTextField(no);
        no.setVisible(false);
        no.setEditable(false);
        cp.add(no);

        algorithmName = new JTextField();
        algorithmName.setBounds(595,488,100,25);
        beautifyTextField(algorithmName);
        algorithmName.setVisible(false);
        algorithmName.setEditable(false);
        cp.add(algorithmName);

        brickNum = new JTextField();
        brickNum.setBounds(595,538,100,25);
        beautifyTextField(brickNum);
        brickNum.setVisible(false);
        brickNum.setEditable(false);
        cp.add(brickNum);

        suspendNum = new JTextField();
        suspendNum.setBounds(595,588,100,25);
        beautifyTextField(suspendNum);
        suspendNum.setVisible(false);
        suspendNum.setEditable(false);
        cp.add(suspendNum);

        exit = new JLabel(new ImageIcon("images/X.png"));
        exit.setBounds(802,328,20,20);
        exit.setVisible(false);
        cp.add(exit);

        saveButton = new JLabel(new ImageIcon("images/保存1.png"));
        saveButton.setBounds(565,312,150,40);
        saveButton.setVisible(false);
        cp.add(saveButton);

        saveWindow  = new JLabel(new ImageIcon("images/保存窗口.png"));
        saveWindow.setBounds(400,311,450,320);
        saveWindow.setVisible(false);
        cp.add(saveWindow);

        /*
        * 原创，设置一个蒙版，避免误点
        * */
        middle = new JTextField();
        middle.setBounds(0,0,1200,900);
        middle.setOpaque(false);
        middle.setVisible(false);
        cp.add(middle);

        /*
        * 主体部分
        * */
        save = new JLabel(new ImageIcon("images/保存结果.png"));
        save.setBounds(90,30,100,40);
        cp.add(save);

        history = new JLabel(new ImageIcon("images/查看历史.png"));
        history.setBounds(320,30,100,40);
        cp.add(history);

        newWindow = new JLabel(new ImageIcon("images/新建窗口.png"));
        newWindow.setBounds(550,30,100,40);
        cp.add(newWindow);

        message1 = new JLabel(new ImageIcon("images/5~20.png"));
        message1.setBounds(365,155,95,30);
        cp.add(message1);

        message2 = new JLabel(new ImageIcon("images/2~9.png"));
        message2.setBounds(1035,265,55,30);
        cp.add(message2);

        inputText = new JTextField();
        inputText.setBounds(90,150,650,40);
        beautifyTextField(inputText);
        cp.add(inputText);

        input = new JLabel(new ImageIcon("images/输入序列.png"));
        input.setBounds(760,150,190,40);
        cp.add(input);

        numberText = new JTextField();
        numberText.setBounds(980,260,160,40);
        beautifyTextField(numberText);
        cp.add(numberText);

        algorithmText = new JTextField("未选择");
        algorithmText.setBounds(315,600,80,40);
        beautifyTextField(algorithmText);
        algorithmText.setEnabled(false);
        cp.add(algorithmText);

        select = new JLabel(new ImageIcon("images/选择算法.png"));
        select.setBounds(90,600,200,40);
        cp.add(select);

        MyTableModel selectModel = new MyTableModel();
        selectModel.addColumn("");
        selectModel.addRow(new Object[]{"FIFO"});
        selectModel.addRow(new Object[]{"URL"});
        selectModel.addRow(new Object[]{"OPT"});
        selectTable = new JTable(selectModel);
        beautifyTable(selectTable);
        selectTable.setRowHeight(40);
        selectTable.setBounds(90,640,200,120);
        selectTable.setVisible(false);
        cp.add(selectTable);

        clear = new JLabel(new ImageIcon("images/清空.png"));
        clear.setBounds(420,600,120,40);
        cp.add(clear);

        animation = new JLabel(new ImageIcon("images/动画.png"));
        animation.setBounds(600,600,120,40);
        cp.add(animation);

        start = new JLabel(new ImageIcon("images/开始执行.png"));
        start.setBounds(760,600,190,40);
        cp.add(start);

        times = new JTextField("0");
        times.setBounds(1072,518,40,40);
        times.setEnabled(false);
        beautifyTextField(times);
        cp.add(times);

        initModel();
        table = new JTable(model);
        beautifyTable(table);
        JScrollPane scroll;
        scroll = new JScrollPane(table);
        scroll.setBorder(new LineBorder(null,0));
        scroll.setBounds(90, 215, 860, 354);
        cp.add(scroll);

        bigText = new JTextField();
        bigText.setBounds(0,0,1200,930);
        beautifyTextField(bigText);
        cp.add(bigText);

        /*
         * 保存结果事件
         * */
        save.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                windowSwitch();
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                save.setIcon(new ImageIcon("images/保存结果1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                save.setIcon(new ImageIcon("images/保存结果.png"));
            }
        });
        saveButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                /*
                 * 这边写将结果添加到数据库
                 * */
                DB dbcon = new DB();
                String sql = "insert into result(ResultName,ResultNo,algorithm,SuspendNum,BrickNum) values(?,?,?,?,?);";
                PreparedStatement prestate;
                if(name.getText().length()>0) {
                    try {
                        prestate = dbcon.PreparedStatement(sql);
                        prestate.setString(1, name.getText());
                        prestate.setString(2, no.getText());
                        prestate.setString(3, algorithmName.getText());
                        prestate.setInt(4, Integer.parseInt(brickNum.getText()));
                        prestate.setInt(5, Integer.parseInt(suspendNum.getText()));
                        prestate.addBatch();
                        prestate.executeBatch();
                        JOptionPane.showMessageDialog(null, "    恭喜，保存成功！");
                        windowSwitch();

                    }
                    catch (SQLException se){
                        JOptionPane.showMessageDialog(null, "     名字重复");
                    }
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                saveButton.setIcon(new ImageIcon("images/保存.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                saveButton.setIcon(new ImageIcon("images/保存1.png"));
            }
        });

        exit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                windowSwitch();
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                exit.setIcon(new ImageIcon("images/X1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                exit.setIcon(new ImageIcon("images/X.png"));
            }
        });
        /*
         * 查看历史事件
         * */
        history.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                history.setIcon(new ImageIcon("images/查看历史1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                history.setIcon(new ImageIcon("images/查看历史.png"));
            }
        });
        /*
         * 新建窗口事件
         * */
        newWindow.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new PageJFrame();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                newWindow.setIcon(new ImageIcon("images/新建窗口1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                newWindow.setIcon(new ImageIcon("images/新建窗口.png"));
            }
        });
        /*
         * 输入序列事件
         * */
        input.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                String str1 = inputText.getText();
                String str2 = numberText.getText();
                if(inputTextOK&&numberTextOK){
                    model = new MyTableModel();
                    for (int i = 0; i <str1.length(); i++) {
                        model.addColumn("");
                    }
                    model.addRow(toArray(str1));
                    for (int i = 0; i <Integer.parseInt(str2); i++) {
                        model.addRow(new Object[]{});
                    }
                    model.addRow(new Object[]{});
                    table.setModel(model);
                    table.setRowHeight(350/(Integer.parseInt(str2)+2));
                    readyOK = true;
                }
                else{
                    JOptionPane.showMessageDialog(null,"两个文本框不能留空");
                }

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                bigText.requestFocus(true);
                input.setIcon(new ImageIcon("images/输入序列1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                input.setIcon(new ImageIcon("images/输入序列.png"));
            }
        });
        /*
         * 选择算法事件
         * */
        select.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                selectTable.setVisible(true);
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                select.setIcon(new ImageIcon("images/选择算法1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                select.setIcon(new ImageIcon("images/选择算法.png"));
            }
        });
        /*
         * 清空事件
         * */
        clear.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                new PageJFrame();
                dispose();
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                clear.setIcon(new ImageIcon("images/清空1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                clear.setIcon(new ImageIcon("images/清空.png"));
            }
        });
        /*
         * 动画事件
         * */
        animation.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                if(readyOK&&chooseOk){
                    System.out.println("动画OK");
                }
                else
                    JOptionPane.showMessageDialog(null,"还没有准备好！\n请选择算法并输入序列");
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                animation.setIcon(new ImageIcon("images/动画1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                animation.setIcon(new ImageIcon("images/动画.png"));
            }
        });
        /*
        * 开始执行事件
        * */
        start.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                if(readyOK&&chooseOk){
                    //下面两句是为了清除之前遗留下来的√
                    model.removeRow(4);
                    model.addRow(new Object[]{});
                    if(algorithmText.getText().equals("FIFO"))
                        FIFO();
                    else if(algorithmText.getText().equals("URL"))
                        URL();
                    else
                        OPT();
                    name.setText("填写名字");
                    no.setText(inputText.getText());
                    algorithmName.setText(algorithmText.getText());
                    brickNum.setText(numberText.getText());
                    suspendNum.setText(times.getText());
                }
                else
                    JOptionPane.showMessageDialog(null,"还没有准备好！\n请选择算法并输入序列");
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                start.setIcon(new ImageIcon("images/开始执行1.png"));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                start.setIcon(new ImageIcon("images/开始执行.png"));
            }
        });
        /*
        * 物理块失去焦点事件
        * */
        numberText.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                message2.setVisible(false);
            }

            @Override
            public void focusLost(FocusEvent e) {
                String str = numberText.getText();
                if(str.length()==0){
                    message2.setVisible(true);
                    numberTextOK = false;
                }
                else if(str.length()>1||str.charAt(0)<'2'||str.charAt(0)>'9'){
                    JOptionPane.showMessageDialog(null,"格式不符合规范\n请输入2~9之间的某个数字");
                    numberText.setText("");
                    message2.setVisible(true);
                    numberTextOK = false;
                }
                else{
                    numberTextOK = true;
                }
            }
        });
        /*
        * 输入序列失去焦点事件
        * */
        inputText.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                message1.setVisible(false);
            }

            @Override
            public void focusLost(FocusEvent e) {
                String str = inputText.getText();
                int flag = 0;
                for (int i = 0; i <str.length(); i++) {
                    if(str.charAt(i)<'0'||str.charAt(i)>'9')
                        flag++;
                }
                if(str.length()==0){
                    message1.setVisible(true);
                    inputTextOK = false;
                }
                else if(str.length()>20||str.length()<5||flag>0){
                    JOptionPane.showMessageDialog(null,"格式不符合规范\n请输入长度为5~20的数字序列");
                    inputText.setText("");
                    message1.setVisible(true);
                    inputTextOK = false;
                }
                else{
                    inputTextOK = true;
                }
            }
        });
        selectTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                String content = selectTable.getValueAt(selectTable.getSelectedRow(),0).toString();
                if(content!=null){
                    algorithmText.setText(content);
                    selectTable.setVisible(false);
                    chooseOk = true;
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });


        this.setVisible(true);
    }
    /*
    * OPT算法
    * */
    private void OPT(){
        String str1 = inputText.getText();
        int rowIndex = Integer.parseInt(numberText.getText())+1;
        int num = rowIndex-1;
        int unFullIndex = rowIndex-1;
        list = new LinkedList<>();
        for (int i = 0; i <unFullIndex; i++) {
            boolean flag = false;//用于记录是否重复

            for (String s : list) {
                if (s.equals(str1.substring(i, i + 1))) {
                    flag = true;
                    break;
                }
            }
            if(!flag){
                    list.add(str1.substring(i, i + 1));
                for (int j = 0; j <list.size(); j++){
                    model.setValueAt(list.get(j),j+1,i);
                }
                model.setValueAt("√",rowIndex,i);
            }
            /*
             * 这边要改
             * */
            else{
                //遇到相同的，不变
                for (int j = 0; j <list.size(); j++){
                    model.setValueAt(list.get(j),j+1,i);
                }
                unFullIndex++;
            }
        }
        //满列
        for (int i = unFullIndex; i <str1.length(); i++) {
            boolean flag = false;//用于记录是否重复
            for (String s : list) {
                if (s.equals(str1.substring(i, i + 1))) {
                    flag = true;
                    break;
                }
            }
            if(flag){
                //遇到相同的,不变
                for (int j = 0; j <list.size(); j++)
                    model.setValueAt(list.get(j),j+1,i);
            }
            else{
                /*
                * 这边要改
                * */
                list.set(replacedIndex(i+1),str1.substring(i,i+1));
                for (int j = 0; j <list.size(); j++){
                    model.setValueAt(list.get(j),j+1,i);
                }
                model.setValueAt("√",rowIndex,i);
                num++;
            }
        }
        table.setModel(model);
        times.setText(String.valueOf(num));
        JOptionPane.showMessageDialog(null,"执行成功！");
    }
    /*
     * URL算法
     * */
    private void URL(){
        String str1 = inputText.getText();
        int rowIndex = Integer.parseInt(numberText.getText())+1;
        int num = rowIndex-1;
        int unFullIndex = rowIndex-1;
        queue = new ArrayBlockingQueue<>(unFullIndex);

        //未满列
        for (int i = 0; i <unFullIndex; i++) {
            boolean flag = false;//用于记录是否重复
            for (int k = 0; k < queue.size(); k++) {
                if(queue.toArray()[k].toString().equals(str1.substring(i, i+1))){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                //容错，若物理块数量过多，字符串下标越界时直接退出函数
                try{
                    queue.add(str1.substring(i,i+1));
                }catch (Exception e){
                    return;
                }
                for (int j = 0; j <queue.size(); j++){
                    model.setValueAt(queue.toArray()[j],j+1,i);
                }
                model.setValueAt("√",rowIndex,i);
            }
            /*
             * 这边要改
             * */
            else{
                //遇到相同的，将最近使用的序号放在队尾，不容易淘汰
                down(queue, str1.substring(i, i + 1));
                for (int j = 0; j <queue.size(); j++){
                    model.setValueAt(queue.toArray()[j],j+1,i);
                }
                unFullIndex++;
            }
        }
        //满列
        for (int i = unFullIndex; i <str1.length(); i++) {
            boolean flag = false;//用于记录是否重复
            for (int k = 0; k <queue.size(); k++) {
                if(queue.toArray()[k].toString().equals(str1.substring(i, i+1)))
                    flag = true;
            }
            if(flag){
                //遇到相同的，将最近使用的序号放在队尾，不容易淘汰
                down(queue, str1.substring(i, i + 1));
                for (int j = 0; j <queue.size(); j++)
                    model.setValueAt(queue.toArray()[j],j+1,i);
            }
            else{
                queue.poll();//队首出队
                queue.add(str1.substring(i,i+1));//队尾入队
                for (int j = 0; j <queue.size(); j++){
                    model.setValueAt(queue.toArray()[j],j+1,i);
                }
                model.setValueAt("√",rowIndex,i);
                num++;
            }
        }
        table.setModel(model);
        times.setText(String.valueOf(num));
        JOptionPane.showMessageDialog(null,"执行成功！");
    }
    /*
    * FIFO算法
    * */
    private  void FIFO(){
        String str1 = inputText.getText();
        int rowIndex = Integer.parseInt(numberText.getText())+1;
        int num = rowIndex-1;
        int unFullIndex = rowIndex-1;
        queue = new ArrayBlockingQueue<>(unFullIndex);

        //未满列
        for (int i = 0; i <unFullIndex; i++) {
            boolean flag = false;//用于记录是否重复
            for (int k = 0; k < queue.size(); k++) {
                if(queue.toArray()[k].toString().equals(str1.substring(i, i+1))){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                //容错，若物理块数量过多，字符串下标越界时直接退出函数
                try{
                    queue.add(str1.substring(i,i+1));
                }catch (Exception e){
                    return;
                }
                for (int j = 0; j <queue.size(); j++){
                    model.setValueAt(queue.toArray()[j],j+1,i);
                }
                model.setValueAt("√",rowIndex,i);
            }
            else{
                for (int j = 0; j <queue.size(); j++){
                    model.setValueAt(queue.toArray()[j],j+1,i);
                }
                unFullIndex++;
            }
        }
        //满列
        for (int i = unFullIndex; i <str1.length(); i++) {
            boolean flag = false;//用于记录是否重复
            for (int k = 0; k <queue.size(); k++) {
                if(queue.toArray()[k].toString().equals(str1.substring(i, i+1)))
                    flag = true;
            }
            //如果重复,则不变
            if(flag){
                for (int j = 0; j <queue.size(); j++)
                    model.setValueAt(queue.toArray()[j],j+1,i);
            }
            else{
                queue.poll();//队首出队
                queue.add(str1.substring(i,i+1));//队尾入队
                for (int j = 0; j <queue.size(); j++){
                    model.setValueAt(queue.toArray()[j],j+1,i);
                }
                model.setValueAt("√",rowIndex,i);
                num++;
            }
        }
        table.setModel(model);
        times.setText(String.valueOf(num));
        JOptionPane.showMessageDialog(null,"执行成功！");
    }

    private void windowSwitch(){
        if(saveWindow.isVisible()){
            saveWindow.setVisible(false);
            saveButton.setVisible(false);
            exit.setVisible(false);
            middle.setVisible(false);
            name.setVisible(false);
            no.setVisible(false);
            algorithmName.setVisible(false);
            brickNum.setVisible(false);
            suspendNum.setVisible(false);
        }
        else
        {
            saveWindow.setVisible(true);
            saveButton.setVisible(true);
            exit.setVisible(true);
            middle.setVisible(true);
            middle.requestFocus(true);
            name.setVisible(true);
            no.setVisible(true);
            algorithmName.setVisible(true);
            brickNum.setVisible(true);
            suspendNum.setVisible(true);
        }
    }
    /*
    * 返回以后最长时间内不访问的元素在list里的下标
    * */
    private  int replacedIndex(int index){
        LinkedList<String>list1 = new LinkedList<>();
        String str = inputText.getText();
        for (int i = index; i <str.length(); i++) {
            //如果不包含相同元素，并且物理块中包含这个元素，则添加
            if(!list1.contains(str.substring(i,i+1))&&list.contains(str.substring(i,i+1)))
                list1.add(str.substring(i,i+1));
        }
        //如果有元素之后再也没有出现过，则将它添加到最后
        for (String s : list) {
            if (!list1.contains(s)) {
                list1.add(s);
                break;
            }
        }
        return list.indexOf(list1.getLast());
    }
    /*
    * 将队列里一个元素移动到队尾，返回这个队列
    * */
    private void down(ArrayBlockingQueue<String> queue,String s){
//        ArrayBlockingQueue<String> queue2 = queue;
        int index = -1;//保存下标
        for (int i = 0; i <queue.size(); i++) {
            if(queue.toArray()[i].toString().equals(s)){
                index=i;
                break;
            }
        }
        //表示第一个元素匹配
        if(index==0){
            queue.poll();
            queue.add(s);
        }
        //表示中间元素匹配
        else {
            queue.remove(s);
            queue.add(s);
        }
    }
    /*
    * 将一个字符串转化为字符数组
    * */
    private Object [] toArray(String str){
        Object [] obj = new Object[str.length()];
        for (int i = 0; i <str.length(); i++) {
            obj[i] = str.charAt(i);
        }
        return obj;
    }
    /*
     * 初始化model
     * */
    private void initModel(){
        model = new MyTableModel();
        for (int i = 0; i <10; i++) {
            model.addColumn("");
        }
        model.addRow(new Object[]{"这","里","表","示","页","面","走","向","序","列"});
        for (int i = 0; i <3; i++) {
            model.addRow(new Object[]{});
        }
        model.addRow(new Object[]{"这","里","表","示","缺","页","中","断","与","否"});
    }
    /*
     * 文本框美化方法复用
     * */
    private void beautifyTextField(JTextField jTextfield){
        jTextfield.setOpaque(false);
        jTextfield.setHorizontalAlignment(JTextField.CENTER);
        jTextfield.setFont(new Font("等线", Font.PLAIN, 22));
        jTextfield.setBorder(new LineBorder(null,0));
    }
    /*
     * 设置一个表格对象,复用
     * */
    private void beautifyTable(JTable table){
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);//设置居中方式
        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);
        table.getTableHeader().setVisible(false);
        table.setRowHeight(70);
        table.setFont(new Font("黑体", Font.PLAIN, 18));
    }

    public static void main(String[] args) {
        new PageJFrame();
    }
}
